package com.vehicle.biz.core.support.abs;

import com.vehicle.biz.core.support.PlateDetectSupport;
import com.vehicle.constants.Constants;
import com.vehicle.domains.ctx.PlateDetectContext;
import com.vehicle.domains.entity.CameraEntity;
import com.vehicle.domains.entity.CommonVehicleEntity;
import com.vehicle.domains.entity.VehicleCommonLedgerEntity;
import com.vehicle.domains.info.InternalVehicleInfo;
import com.vehicle.exception.VehicleAccessException;
import com.vehicle.service.VehicleCommonLedgerService;
import com.vehicle.utils.Base64ToImageUtil;
import jakarta.annotation.Resource;

import java.util.Date;

public abstract class AbstractSupport implements PlateDetectSupport {
    @Resource
    private VehicleCommonLedgerService vehicleCommonLedgerService;
    public void processIn(PlateDetectContext plateDetectContext) {
        CommonVehicleEntity commonVehicleEntity = plateDetectContext.getCommonVehicleEntity();
        if (commonVehicleEntity == null) {
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("车辆不存在");
        }
        if (!commonVehicleEntity.getEnabled()) {
            throw VehicleAccessException.SYSTEM_ERROR.newInstance("车辆已失效");
        }
        if (commonVehicleEntity.getExpireTime().getTime() < System.currentTimeMillis()) {
            throw VehicleAccessException.SYSTEM_ERROR.newInstance("车辆已过期");
        }
    }

    @Override
    public void recordIn(PlateDetectContext plateDetectContext) {
        VehicleCommonLedgerEntity vehicleCommonLedgerEntity = new VehicleCommonLedgerEntity();

        InternalVehicleInfo internalVehicleInfo = plateDetectContext.getInternalVehicleInfo();
        String vehicleImgBase64 = internalVehicleInfo.getVehicleImgBase64();
        String plateImgBase64 = internalVehicleInfo.getPlateImgBase64();
        vehicleCommonLedgerEntity.setVehicleNumber(plateDetectContext.getVehicleNumber());
        vehicleCommonLedgerEntity.setVehicleType(plateDetectContext.getVehicleType());
        vehicleCommonLedgerEntity.setEntryVehicleImgUrl(Base64ToImageUtil.saveBase64(vehicleImgBase64, null));
        vehicleCommonLedgerEntity.setEntryPlateImgUrl(Base64ToImageUtil.saveBase64(plateImgBase64,null));
        vehicleCommonLedgerEntity.setEntryTime(new Date());
        vehicleCommonLedgerEntity.setEntryOperator(Constants.SYSTEM_OPERATOR);
        CameraEntity cameraEntity = plateDetectContext.getCameraEntity();
        vehicleCommonLedgerEntity.setEntryCameraIp(cameraEntity.getCameraIp());
        vehicleCommonLedgerEntity.setEntryRoadCode(cameraEntity.getRoadCode());
        vehicleCommonLedgerEntity.setEntryExitCode(cameraEntity.getEntryExitCode());

        vehicleCommonLedgerService.insert(vehicleCommonLedgerEntity);
        plateDetectContext.setVehicleCommonLedgerEntity(vehicleCommonLedgerEntity);
    }

    @Override
    public void recordOut(PlateDetectContext plateDetectContext) {
        //先查询数据库是否有该车辆的台账信息
        VehicleCommonLedgerEntity lastVehicleCommonLedgerEntity =vehicleCommonLedgerService.getLastLedgerByVehicleNumber(plateDetectContext.getVehicleNumber());

        //创建新的台账信息
        VehicleCommonLedgerEntity vehicleCommonLedgerEntity = new VehicleCommonLedgerEntity();
        vehicleCommonLedgerEntity.setVehicleNumber(plateDetectContext.getVehicleNumber());
        vehicleCommonLedgerEntity.setVehicleType(plateDetectContext.getVehicleType());
        // 设置出场时间与操作人信息
        vehicleCommonLedgerEntity.setExitTime(new Date());
        vehicleCommonLedgerEntity.setExitOperator(Constants.SYSTEM_OPERATOR);

        // 保存出场车辆图片与车牌图片至本地，存储路径写入数据库
        vehicleCommonLedgerEntity.setExitVehicleImgUrl(Base64ToImageUtil.saveBase64(plateDetectContext.getInternalVehicleInfo().getVehicleImgBase64(),null));
        vehicleCommonLedgerEntity.setExitPlateImgUrl(Base64ToImageUtil.saveBase64(plateDetectContext.getInternalVehicleInfo().getPlateImgBase64(),null));

        // 获取当前识别事件对应的摄像头信息（包含IP、道路、进出口编码等）
        CameraEntity cameraEntity = plateDetectContext.getCameraEntity();
        vehicleCommonLedgerEntity.setExitCameraIp(cameraEntity.getCameraIp());
        vehicleCommonLedgerEntity.setExitRoadCode(cameraEntity.getRoadCode());
        vehicleCommonLedgerEntity.setExitEntryExitCode(cameraEntity.getEntryExitCode());

        //如果上一次车辆台账为空或者已经出场了，则直接插入
        if (lastVehicleCommonLedgerEntity == null || lastVehicleCommonLedgerEntity.getExitTime() != null) {
            vehicleCommonLedgerService.insert(vehicleCommonLedgerEntity);
        } else {
            //否则，更新车辆台账信息
            vehicleCommonLedgerEntity.setId(lastVehicleCommonLedgerEntity.getId());
            vehicleCommonLedgerService.updateVehicleExitInfo(vehicleCommonLedgerEntity);
        }

        //设置到上下文
        plateDetectContext.setVehicleCommonLedgerEntity(vehicleCommonLedgerEntity);
    }
}
